package srzy.smcache

import srzy.common.Logging
import srzy.common.ShellExecute

object CacheUtil extends Logging {

  def operCache(cacheNum: Int, l: List[String]) = {
    if (l.length != 0) {
      val str1 = """expect -c "
spawn /home/hn_sm/ocssm/bin/CacheClient /home/hn_sm/ocssm/config/server_cfg_""" + cacheNum + """;
expect \"Connect Mode:\" {send \"\r\"}
expect \"name :\" {send \"sm\r\"}
expect \"Password :\" {send \"sm\r\"}
expect \"Serv id :\" {send \"""" + cacheNum + """00001\r\"}
"""

      val str2 = l.map(x => """expect \"SMCache>\" {send \"""" + x + """;\r\"}""" + "\n").mkString("\n")
      val str3 = """expect \"SMCache>\" {send \"quit\r\"}
"
"""
      val str = str1 + str2 + str3
      new ShellExecute {
        val shell = str
      }.execute
    }
  }

  def cleanCache(cacheNum: Int) = {

    val str = """expect -c "
spawn /home/hn_sm/ocssm/bin/CacheClient /home/hn_sm/ocssm/config/server_cfg_""" + cacheNum + """;
expect \"Connect Mode:\" {send \"\r\"}
expect \"name :\" {send \"sm\r\"}
expect \"Password :\" {send \"sm\r\"}
expect \"Serv id :\" {send \"""" + cacheNum + """00001\r\"}
expect \"SMCache>\" {send \"TRUNCATE table SM_VOICE_SESSION_STORE;\r\"}
expect \"SMCache>\" {send \"TRUNCATE table SM_P2PSMS_STORE;\r\"}
expect \"SMCache>\" {send \"TRUNCATE table SM_DATA_SESSION_STORE;\r\"}
expect \"SMCache>\" {send \"TRUNCATE table SM_ISMP_SESSION_STORE;\r\"}
expect \"SMCache>\" {send \"TRUNCATE table SM_DSL_SESSION_STORE;\r\"}
expect \"SMCache>\" {send \"TRUNCATE table SM_VOICE_SESSION_STORE;\r\"}
expect \"SMCache>\" {send \"TRUNCATE table SM_DATA_RG_STORE;\r\"}
expect \"SMCache>\" {send \"TRUNCATE table TB_PRD_PRD_INST;\r\"}
expect \"SMCache>\" {send \"quit\r\"}
"
"""

    new ShellExecute {
      val shell = str
    }.execute
  }
}